package jbp.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import jbp.common.util.JsonUtil;
import jbp.domain.version.AddVersionDto;
import jbp.domain.version.EditVersionDto;
import jbp.domain.version.VersionDetailDto;
import jbp.domain.version.VersionListDto;
import jbp.entity.VersionDetailEntity;
import jbp.entity.VersionEntity;
import jbp.mapper.VersionMapper;
import jbp.service.JbpBaseService;
import jbp.service.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class VersionServiceImpl extends JbpBaseService implements VersionService {
    @Autowired
    private VersionMapper versionMapper;

    public VersionDetailDto Query(int id) {
        VersionDetailDto detailDto = versionMapper.SelById(id).toDetailDto();
        detailDto.setVersionDetailLst(versionMapper.SelByVersionId(id));
        return detailDto;
        //return versionMapper.SelById(id).toDetailDto();
    }

    public int Count(Map<String, Object> params) {
        return versionMapper.getCount(params);
    }

    public List<VersionListDto> Query(Map<String, Object> params) {
        List<VersionEntity> entityList = versionMapper.SelList(params);
        List<VersionListDto> dtoList = new ArrayList<VersionListDto>();
        for(int i=0; i<entityList.size(); i++){
            VersionEntity entity = entityList.get(i);
            entity.setVersionDetailLst(
                    versionMapper.SelByVersionId(entity.getId())
            );
            dtoList.add(entity.toListDto());
        }
        return dtoList;
    }

    public boolean Add(AddVersionDto addDto) {
        boolean flag = false;
        List<VersionDetailEntity> jsonLst = new ArrayList<VersionDetailEntity>();
        if(!addDto.getDetailJsonStr().isEmpty()){
            jsonLst = JsonUtil.toObject(addDto.getDetailJsonStr()
                            , new TypeReference<List<VersionDetailEntity>>() {});

        }
        System.out.println(jsonLst.size());
        try {
            VersionEntity entity = addDto.toEntity();
            entity.setVersionDetailLst(jsonLst);
            versionMapper.Add(entity);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean Update(EditVersionDto editDto) {
        boolean flag = false;
        List<VersionDetailEntity> jsonLst = new ArrayList<VersionDetailEntity>();
        if(!editDto.getDetailJsonStr().isEmpty()){
            jsonLst = JsonUtil.toObject(editDto.getDetailJsonStr()
                            , new TypeReference<List<VersionDetailEntity>>() {});

        }
        System.out.println(jsonLst.size());
        try {
            VersionEntity entity = editDto.toEntity();
            entity.setVersionDetailLst(jsonLst);
            versionMapper.Update(entity);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean Delete(String id) {
        boolean flag = false;
        try{
            versionMapper.Delete(id);
            flag = true;
        } catch(Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
